package 锁;

/**
 * @Author zhang lei
 * @Date 2022-01-21 15:25
 */
public class ThreadLocalQuestion {
    static class DemoClass {
        private ThreadLocal<Integer> i = new ThreadLocal<>() {
            Integer integer;

            @Override
            protected Integer initialValue() {
                return 0;
            }
        };

        private Integer get() {
            return i.get();
        }

        private void set(Integer integer) {
            i.set(i.get()+integer);
        }
    }

    public static void main(String[] args) throws InterruptedException {
        DemoClass demoClass = new DemoClass();
        for (int i = 1; i <= 100; i++) {
            new Thread(()->{
                System.out.println(Thread.currentThread().getName()+ "刚进来 demoClass.get() = " + demoClass.get());
                for (int i1 = 1; i1 <= 100; i1++) {
                    demoClass.set(i1);
                }
                System.out.println(Thread.currentThread().getName()+ ": demoClass.get() = "+demoClass.get());
            }).start();
        }
        Thread.sleep(Integer.MAX_VALUE);
    }
}
